//
//  LTMessageVideoBody.m
//  LTIM
//
//  Created by 俊松何 on 2017/4/13.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTMessageVideoBody.h"

static const NSString *videoPath = @"chatvideobuffer";
static const NSString *videocoverPath = @"chatvideocoverbuffer";

@implementation LTMessageVideoBody


- (instancetype)initWithVideoData:(NSData *)aData
                       coverImage:(UIImage *)coverimage
                    duration:(NSInteger)duration
{
    if(self = [super init])
    {
        
        _coverH = coverimage.size.height;
        _coverW = coverimage.size.width;
        
        NSData *coverdata = UIImageJPEGRepresentation(coverimage, 0.3);
        [self saveVideoData:aData];
        [self saveCoverimageData:coverdata];
        _videodownloadStatus = LTDownloadStatusSuccessed;
        _thumbnaildownloadStatus = LTDownloadStatusSuccessed;
        
       
        
        
        
    }
    return self;
    
}

-(NSString *)localvideoPath
{
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    _localvideoPath = [NSString stringWithFormat:@"%@/%@",documentPath,_videoLocalrelativePath];
    
    return _localvideoPath;

}
-(NSString *)localvideoCoverPath
{
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    _localvideoCoverPath = [NSString stringWithFormat:@"%@/%@",documentPath,_coverLocalrelativePath];
    return _localvideoCoverPath;
}

-(void)saveCoverimageData:(NSData *)data
{
    
    int random = arc4random() % 1000;
    
    
    NSString *filename = [NSString stringWithFormat:@"cover%d%ld.jpg",random, (NSInteger)[[NSDate date] timeIntervalSince1970] * 1000];
    
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    
    documentPath = [NSString stringWithFormat:@"%@/%@",documentPath,videocoverPath];
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:documentPath])
    {
        [fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *failePath = [NSString stringWithFormat:@"%@/%@",documentPath,filename];
    _coverLocalrelativePath = [NSString stringWithFormat:@"%@/%@",videocoverPath,filename];
    
    
    BOOL suc = [fileManager createFileAtPath:failePath contents:data attributes:nil];
    
            NSLog(@"videosuc = %d",suc);

    
}

-(void)saveVideoData:(NSData *)data
{
    
    int random = arc4random() % 1000;
    NSString *filename = [NSString stringWithFormat:@"video%d%ld.mp4",random, (NSInteger)[[NSDate date] timeIntervalSince1970] * 1000];
    
    NSArray *arrDocumentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentPath=[arrDocumentPaths objectAtIndex:0];
    
    documentPath = [NSString stringWithFormat:@"%@/%@",documentPath,videoPath];
    
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if(![fileManager fileExistsAtPath:documentPath])
    {
        [fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSString *failePath = [NSString stringWithFormat:@"%@/%@",documentPath,filename];
    _videoLocalrelativePath = [NSString stringWithFormat:@"%@/%@",videoPath,filename];
    
    
    BOOL suc = [fileManager createFileAtPath:failePath contents:data attributes:nil];
    
    NSLog(@"coversuc = %d",suc);
    
}




@end
